﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

'From Custom Classes/Reference
Imports StedySoft.SenseSDK
Imports StedySoft.SenseSDK.DrawingCE

#End Region

Namespace SenseButtonControlEx

    ''' <summary>SenseButtonControlEx.HTCButtonStyle1</summary>
    ''' <author>Greenhacks</author>
    ''' <author-reference>None</author-reference>
    ''' <copyright>None</copyright>
    ''' <version>0.1</version>
    ''' <remarks>None</remarks>
    ''' <reference>None</reference>
    Public Class HTCButtonStyle1

        ''' <summary>
        ''' Check if it is VGA or QVGA
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _isVGA() As Boolean

            Try

                'Return the output
                Return StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' _getIImageFromResource
        ''' </summary>
        ''' <param name="resource"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _getIImageFromResource(ByVal resource As String) As IImage
            Dim iimg As IImage = Nothing
            Using strm As MemoryStream = If(_isVGA, SenseSDKExtended.Resources.VGA.Images.GetIImageFromResource(resource), SenseSDKExtended.Resources.QVGA.Images.GetIImageFromResource(resource))
                ImagingFactory.GetImaging.CreateImageFromBuffer(strm.GetBuffer, Convert.ToUInt32(strm.Length), BufferDisposalFlag.BufferDisposalFlagNone, iimg)
            End Using
            Return iimg
        End Function

#Region "Fields"

        Private _text As String = Nothing

        Private _buttonEnabled As IImage = Nothing
        Private _buttonPressed As IImage = Nothing

        Private _buttonEnabledLeft As IImage = _getIImageFromResource("button.enabled.gray.left")
        Private _buttonEnabledMiddle As IImage = _getIImageFromResource("button.enabled.gray.middle")
        Private _buttonEnabledRight As IImage = _getIImageFromResource("button.enabled.gray.right")

        Private _buttonPressedLeft As IImage = _getIImageFromResource("button.pressed.black.left")
        Private _buttonPressedMiddle As IImage = _getIImageFromResource("button.pressed.black.middle")
        Private _buttonPressedRight As IImage = _getIImageFromResource("button.pressed.black.right")

        Private _clickedButton As Boolean = False
        Private _myCurrentPosition As Integer = 0

#End Region

#Region "Events"

        Public Shadows Event OnClick(ByVal sender As Object)

#End Region

#Region "Methods"

        ''' <summary>
        ''' HTCButtonStyle1_MouseDown
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub HTCButtonStyle1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

            'Record current control position Y
            _myCurrentPosition = e.Y

            _clickedButton = True
            Refresh()

        End Sub

        ''' <summary>
        ''' HTCButtonStyle1_MouseUp
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub HTCButtonStyle1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

            'Check controls current position
            If Not e.Y = _myCurrentPosition Then

                _clickedButton = False
                Refresh()

                'Then exit sub to avoid raising other events
                Exit Sub

            End If

            _clickedButton = False
            Refresh()

            RaiseEvent OnClick(Me)

        End Sub

        ''' <summary>
        ''' HTCButtonStyle1_Paint
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub HTCButtonStyle1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

            Try

                Dim __button As IImage = If(_clickedButton, _buttonPressed, _buttonEnabled)
                If Not _buttonEnabled Is Nothing Then

                    e.Graphics.DrawImageAlphaChannel(__button, New Rectangle(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, ClientRectangle.Height))

                Else

                    If _clickedButton Then

                        e.Graphics.DrawImageAlphaChannel(_buttonPressedLeft, New Rectangle(ClientRectangle.Left, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Height))
                        e.Graphics.DrawImageAlphaChannel(_buttonPressedMiddle, New Rectangle(ClientRectangle.Left + SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Width, ClientRectangle.Top, ClientRectangle.Width - SenseAPIs.SenseImage.GetIImageSize(_buttonPressedLeft).Width - SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedMiddle).Height))
                        e.Graphics.DrawImageAlphaChannel(_buttonPressedRight, New Rectangle(ClientRectangle.Width - SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Width, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonPressedRight).Height))

                    Else

                        e.Graphics.DrawImageAlphaChannel(_buttonEnabledLeft, New Rectangle(ClientRectangle.Left, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Height))
                        e.Graphics.DrawImageAlphaChannel(_buttonEnabledMiddle, New Rectangle(ClientRectangle.Left + SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Width, ClientRectangle.Top, ClientRectangle.Width - SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledLeft).Width - SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledMiddle).Height))
                        e.Graphics.DrawImageAlphaChannel(_buttonEnabledRight, New Rectangle(ClientRectangle.Width - SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Width, ClientRectangle.Top, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Width, SenseAPIs.SenseImage.GetIImageSize(_buttonEnabledRight).Height))

                    End If

                End If


            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

            Try

                ' draw the primary text
                Using f As Font = New Font("Helvetica Neue OTS", 10.0F, FontStyle.Regular)
                    Dim s As String = _text
                    Dim r As Rectangle = New Rectangle(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, ClientRectangle.Height)
                    Using b As Brush = New SolidBrush(If(_clickedButton, Color.White, Color.Black))
                        Using sf As StringFormat = New StringFormat()
                            sf.Alignment = StringAlignment.Center
                            sf.LineAlignment = StringAlignment.Center
                            e.Graphics.DrawString(s, f, b, r, sf)
                        End Using
                    End Using
                End Using

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

#End Region

#Region "Properties"

        ''' <summary>
        ''' Text
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shadows Property Text() As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property

        ''' <summary>
        ''' ButtonEnabledImage
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <CLSCompliant(False)> _
        Public Property ButtonEnabledImage() As IImage
            Get
                Return _buttonEnabled
            End Get
            Set(ByVal value As IImage)
                _buttonEnabled = value
            End Set
        End Property

        ''' <summary>
        ''' ButtonPressedImage
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <CLSCompliant(False)> _
        Public Property ButtonPressedImage() As IImage
            Get
                Return _buttonPressed
            End Get
            Set(ByVal value As IImage)
                _buttonPressed = value
            End Set
        End Property

#End Region

    End Class

End Namespace